package com.article.controller;

import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.article.common.Md5Utils;
import com.article.common.PageParam;
import com.article.common.ResultBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.article.model.User;
import com.article.service.IUserService;


/**
 * @author lx
 */
@Controller
@RequestMapping("/user")
public class UserController {
	 private static final Logger log=LoggerFactory.getLogger(UserController.class);
	 @Resource
	 private IUserService service;

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "/user";
    }

    /**
     * 新增
     *
     * @return
     */
    @RequestMapping("/add")
    public String addData() {
        return "/userAdd";
    }

    /**
     * 修改
     *
     * @return
     */
    @RequestMapping("/edit")
    public String editData(Model model, Integer id) {
        model.addAttribute("user",this.service.getOne(id));
        return "/userEdit";
    }

    /**
     * 登录接口
     * @author lx
     * @date 2021/1/20 10:56
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody login(HttpServletRequest request, String password, String username){
        User user = service.getOneByAccount(username);
        if (user==null){
            return ResultBody.error("当前用户名未注册！");
        }
        if (!Md5Utils.encodeByMd5(password).equals(user.getPassword())){
            return ResultBody.error("用户名密码不正确！");
        }
        request.getSession().setAttribute("USER_SESSION",user);
        return ResultBody.success(user);
    }

    /**
     * 退出接口
     * @author lx
     * @date 2021/1/20 10:56
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody logout(HttpServletRequest request){
        request.getSession().removeAttribute("USER_SESSION");
        return ResultBody.success();
    }


    /**
     * 用户注册
     * @param user
     * @author lx
     * @date 2021/1/20 16:54
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody register(User user){
        user.setUserType(2);
        user.setCreateTime(new Date());
        user.setPassword(Md5Utils.encodeByMd5(user.getPassword()));
        User checkUserName = this.service.getOneByAccount(user.getAccount());
        if (checkUserName!=null){
            return ResultBody.error("用户名已存在！");
        }
        this.service.add(user);
        return ResultBody.success("注册成功，快去登录吧！");
    }

    @RequestMapping("list")
    @ResponseBody
    public PageParam list(PageParam pageParam) {
        return this.service.queryAll(pageParam);
    }

    /**
     * 新增
     * @param user
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody add(User user){
        user.setUserType(2);
        user.setPassword(Md5Utils.encodeByMd5(user.getPassword()));
        user.setCreateTime(new Date());
        this.service.add(user);
        return ResultBody.success("添加成功");
    }

    /**
     * 修改
     * @param user
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody update(User user){
        user.setUpdateTime(new Date());
        this.service.update(user);
        return ResultBody.success("修改成功");
    }

    /**
     * 查找
     * @param id
     */
    @RequestMapping(value = "/getOne")
    @ResponseBody
    public ResultBody getOne(Integer id){
        User obj=this.service.getOne(id);
        if(obj!=null){
            return ResultBody.success(obj);
        }else{
            return ResultBody.error("未获取到数据！");
        }
    }

    /**
     * 删除单条
     * @param id
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody delete(Integer  id){
        this.service.delete(id);
        return ResultBody.success("删除成功");
    }

    /**
     * 删除多条
     * @param ids
     */
    @RequestMapping(value = "/deleteByIds",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody deleteByIds(Integer[]  ids){
        this.service.deleteByIds(ids);
        return ResultBody.success("删除成功");
    }

    @RequestMapping(value = "/restPassword",method = RequestMethod.POST)
    @ResponseBody
    public ResultBody restPassword(Integer[]  ids){
        this.service.restPassword(ids);
        return ResultBody.success("密码成功重置，重置后密码为：123456");
    }

}
